using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using KNCApp.Business.Models;
using KNCApp.Business.Interfaces;

namespace KNCApp.Business
{
    class M5Parser : IDataParser
    {
        private string RES_EXPR = Business.SettingsMgr.GetM5ResultFormat();
        //private string RES_EXPR =   @"^(M|\-)E\d{3}R\d{3}I\d{2}Y\d{2}S\d{3}P\d{3}Z\d{2}$";
        private string CHART_EXPR = Business.SettingsMgr.GetM5ChartFormat();
                //@"^c\d{2}p\d{2}$";
        private char[] CHART_SEPS = new char[]{ 'c', 'p'};
        private char[] RES_SEPS = new char[] { 'E', 'R', 'I', 'Y', 'S', 'P', 'Z' };

        private Regex resRE;
        private Regex chartRE;

        public M5Parser()
        {
            resRE = new Regex(RES_EXPR);
            chartRE = new Regex(CHART_EXPR);
        }

        public AbstractObj Parse(string str)
        {
            str = str.Trim();
           /* StringBuilder sb = new StringBuilder();
            foreach(char c in str1)
            {
                sb.Append(c);
                Program.LogDebug("Result Data ----- - " + c.ToString());
                Program.LogDebug("Result Data +++++ - " + sb.ToString());
            }*/

            Program.LogDebug("Result Data Parse - " + str);
            AbstractObj retObj = null;
            string[] tokens = null;
            if (chartRE.IsMatch(str))
            {
                Program.LogDebug("Chart Data matched - " + str);
                tokens = str.Split(CHART_SEPS, StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Length > 1)
                {
                    ChartResult cr = new ChartResult();
                    cr.ID = -1;
                    cr.CAP = System.Convert.ToInt32(tokens[0]);
                    cr.PLE = System.Convert.ToInt32(tokens[1]);
                    retObj = cr;
                }
            }
            else if (resRE.IsMatch(str))
            {
                Program.LogDebug("Result Data matched - " + str);
                tokens = str.Split(RES_SEPS);
                if (tokens.Length > 7)
                {
                    Result r = new Result();
                    r.ID = -1;
                    r.ETCO2 = System.Convert.ToDecimal(tokens[1]);
                    r.Breath = System.Convert.ToInt32(tokens[2]);
                    if (tokens[5].StartsWith("-"))
                        r.SPO2 = 0;
                    else
                        r.SPO2 = System.Convert.ToInt32(tokens[5]);
                    if (tokens[6].StartsWith("-"))
                        r.Pulse = 0;
                    else
                        r.Pulse = System.Convert.ToInt32(tokens[6]);
                    retObj = r;
                }
            }
            Program.LogDebug("Result Data Parse end - " + retObj);
            return retObj;

        }
        public string GetMode()
        {
            return "5";
        }

    }
}
